import pandas as pd
import os
import sys

FILE_ABS_PATH = os.path.dirname(__file__)
ROOT_PATH = os.path.join(FILE_ABS_PATH, os.pardir)
sys.path.append(ROOT_PATH)
from script_convert_rating import filter_unused_data


def convert_ratings(input_fname, output_fname, columns):
    rating = pd.read_csv(input_fname, sep=",", index_col=None, header=None)
    rating.columns = columns
    rating.drop(columns=['timestamp'], inplace=True)
    order_l = ['userID', 'itemID', 'rating']
    rating = rating[order_l]

    rating = filter_unused_data.convert_ratings(rating)
    filter_unused_data.stats_rating(rating)

    order_l = ['userID', 'itemID', 'rating']
    rating = rating[order_l]
    rating.to_csv(output_fname, index=False)
    print("complete {}".format(output_fname))


if __name__ == '__main__':
    # input_fname = '/home/bianzheng/Dataset/Recommendation/amazon/ratings_Electronics.csv'
    # output_fname = '/home/bianzheng/rec2-mips/intermediate-rating-csv/amazon-electronics.csv'
    # convert_ratings(input_fname=input_fname, output_fname=output_fname)

    input_fname = '/home/bianzheng/Dataset/Recommendation/amazon/ratings_Home_and_Kitchen.csv'
    output_fname = '/home/bianzheng/rec2-mips/intermediate-rating-csv/amazon-home-kitchen.csv'
    convert_ratings(input_fname=input_fname, output_fname=output_fname,
                    columns=['userID', 'itemID', 'rating', 'timestamp'])

    input_fname = '/home/bianzheng/Dataset/Recommendation/amazon/Office_Products.csv'
    output_fname = '/home/bianzheng/rec2-mips/intermediate-rating-csv/amazon-office-products.csv'
    convert_ratings(input_fname=input_fname, output_fname=output_fname,
                    columns=['itemID', 'userID', 'rating', 'timestamp'])
